package com.pktechan.api.service.impl;

import com.pktechan.api.base.BaseMapper;
import com.pktechan.api.base.BaseServiceImpl;
import com.pktechan.api.dao.CustCarriageFlowMapper;
import com.pktechan.api.entity.CustCarriage;
import com.pktechan.api.entity.CustCarriageFlow;
import com.pktechan.api.enumeration.CustCarriageFlowBusinessTypeEnum;
import com.pktechan.api.enumeration.CustCarriageFlowStatusEnum;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.api.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.api.sendMiniMessage.bean.FactoryCarriageApplyMessage;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.service.CustCarriageFlowService;
import com.pktechan.api.service.CustCarriageService;
import com.pktechan.api.service.SendMessageService;
import com.pktechan.api.vo.CustCarriageFlowVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("custCarriageFlowService")
@Transactional
public class CustCarriageFlowServiceImpl extends BaseServiceImpl<CustCarriageFlow> implements CustCarriageFlowService {
    private static final Logger logger = LoggerFactory.getLogger(CustCarriageFlowServiceImpl.class);

    @Autowired
    private CustCarriageFlowMapper custCarriageFlowMapper;
    @Autowired
    private CustCarriageService custCarriageService;
    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<CustCarriageFlow> getBaseMapper() {
        return custCarriageFlowMapper;
    }

    /**
     * 根据订单ID查询应得的运费流水
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public List<CustCarriageFlow> getGenerateListByTradeOrderId(Integer tradeOrderId) {
        return custCarriageFlowMapper.getGenerateListByTradeOrderId(tradeOrderId);
    }

    /**
     * 查看运费流水
     * @param custId
     * @return
     */
    @Override
    public List<CustCarriageFlowVO> getCustCarriageFlowList(Integer custId) {
        return custCarriageFlowMapper.getCustCarriageFlowList(custId);
    }

    /**
     * 根据custId获取运费兑付流水
     * @param custCarriageFlow
     * @return
     */
    @Override
    public List<CustCarriageFlow> getCarriageWithdrawalFlowList(CustCarriageFlow custCarriageFlow) {
        return custCarriageFlowMapper.getCarriageWithdrawalFlowList(custCarriageFlow);
    }

    /**
     * 运费提现申请
     * @param custCarriageFlow
     */
    @Override
    public void freightWithdrawalApply(CustCarriageFlow custCarriageFlow) {
        logger.info("根据id"+custCarriageFlow.getCustCarriageId()+"查询配送商运费表");
        CustCarriage byId = custCarriageService.getById(custCarriageFlow.getCustCarriageId());
        if(byId == null){
            throw new CustomException("未查询到配送商信息");
        }
        custCarriageFlow.setCustId(byId.getCustId());//custId
        custCarriageFlow.setBalancePre(byId.getAvailableBalance());//变动前运费
        custCarriageFlow.setBalanceAfter(byId.getAvailableBalance().subtract(custCarriageFlow.getAmount()));//变动前运费-变动的运费=变动后运费
        custCarriageFlow.setBusinessType(CustCarriageFlowBusinessTypeEnum.WITHDRAWAL.getCode());// --20 提现
        custCarriageFlow.setStatus(CustCarriageFlowStatusEnum.TO_BE_CONFIRMED.getCode());// 待确认
        logger.info("添加运费流水记录");
        custCarriageFlowMapper.save(custCarriageFlow);
        CustCarriage custCarriage = new CustCarriage();
        custCarriage.setId(custCarriageFlow.getCustCarriageId());
        custCarriage.setAvailableBalance(byId.getAvailableBalance().subtract(custCarriageFlow.getAmount()));
        logger.info("修改配送商运费表信息");
        custCarriageService.update(custCarriage);
        //厂家接收-配送商-运费提现-提现申请通知
        FactoryCarriageApplyMessage factoryCarriageApplyMessage = sendMessageService.getFactoryCarriageApplyMessage(custCarriageFlow.getId());
        SendMiniMessageStrategy factoryReferrerCommissionApplyStrategy = BaseSendMiniMessage.getSendMiniMessage(SendMiniMessageEnum.FACTORY_CARRIAGE_APPLY);
        factoryReferrerCommissionApplyStrategy.sendMiniMessageToActiveMq(factoryCarriageApplyMessage);
    }
}
